/**
 * <b>项目名：</b>hclv-web<br/>
 * <b>包名：</b>com.flea.hclv.service.impl<br/>
 * <b>文件名：</b>HTravelGroupServiceImpl.java<br/>
 * <b>版本信息：</b>1.0<br/>
 * <b>日期：</b>2018年11月9日 下午7:08:55<br/>
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HTravelGroupMapper;
import com.flea.hclv.model.entity.HTravelGroup;
import com.flea.hclv.model.entity.vo.VisitorGroupVo;
import com.flea.hclv.service.HTravelGroupService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.dao.OrgRoleMapper;
import com.flea.system.entity.OrgDept;
import com.flea.system.entity.OrgRole;
import com.flea.system.platform.session.FleaOrgUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description
 * @createTime 2018年11月9日 下午7:08:55
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */


@Service("travelGroupService")
public class HTravelGroupServiceImpl implements HTravelGroupService {
    @Autowired
    private HTravelGroupMapper travelGroupMapper;
    @Autowired
    private OrgRoleMapper orgRoleMapper;


    @Override
    public ServerResponse<FleaPage<HTravelGroup>> findMyGroupForPage(HttpServletRequest request, String model, int curPage, int pageSize) throws Exception {
        VisitorGroupVo vvo = JSONObject.parseObject(model, VisitorGroupVo.class);
        RowBounds rowBounds = new RowBounds(curPage, pageSize);
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        if (currentUserDept == null) {
            return ServerResponse.createByErrorMessage("部门不能为空");
        }
        String serviceType = currentUserDept.getServiceType();
        String userId = FleaOrgUtils.getCurUserUuid(request);
        List<HTravelGroup> groups;
        int roleCode = 0;
        List<OrgRole> orgRoles = orgRoleMapper.findByUserUuid(userId);
        for (OrgRole role : orgRoles) {
            String roleCode1 = role.getRoleCode();
            if (roleCode1.equals(HConstant.HCLV_GAID_ADMIN)) {
                roleCode = 1;
                break;
            }
        }
        if (roleCode == 1) {
            //导游管理员用户
            roleCode = 1;
            if (vvo==null){
                vvo = new VisitorGroupVo();
            }
            vvo.setGroupStatus(HConstant.HCLV_VISITOR_GROUP_ISNO);
            vvo.setGroupIsYesStatus(HConstant.HCLV_VISITOR_GROUP_ISYES);
            groups = travelGroupMapper.findAllGroupPaging(rowBounds, vvo);
            for (HTravelGroup group : groups) {
                group.setRoleCode(roleCode);
            }
            FleaPage<HTravelGroup> result = new FleaPage<>(groups);
            return ServerResponse.createBySuccess("成功", result);
        } else if (HConstant.D_SERVICE_ORG_GROUP.equals(serviceType)) {
            //组团社
            roleCode = 2;
            groups = travelGroupMapper.findByMyOrgGroupPaging(rowBounds, currentUserDept.getDeptUuid(), vvo);
            for (HTravelGroup group : groups) {
                group.setRoleCode(roleCode);
            }
            FleaPage<HTravelGroup> result = new FleaPage<>(groups);

            return ServerResponse.createBySuccess("成功", result);
        }
/*		if(null!=vvo&& null!=vvo.getDate()){
			List<HTimeTable> timeTables= timeTableMapper.findByDateForRecoder(vvo.getDate());
			if(null!=timeTables && timeTables.size()>0){
				vvo.setTimeTables(timeTables);
			}
		}*/
        return ServerResponse.createByErrorMessage("没有权限");
    }

    @Override
    public ServerResponse<FleaPage<HTravelGroup>> findMyBackGroupForPage(int curPage, int pageSize, HttpServletRequest request, VisitorGroupVo vvo, String serviceType) {
        RowBounds rowBounds = new RowBounds(curPage, pageSize);
        String orgId = FleaOrgUtils.getCurrentUserDeptUuid(request);
        List<HTravelGroup> groups = travelGroupMapper.findByMyOrgGroupPaging(rowBounds, orgId, vvo);
        FleaPage<HTravelGroup> result = new FleaPage<>(groups);
        return ServerResponse.createBySuccess("成功", result);
    }

  @Override
  public List<HTravelGroup> findByTimeTableId(String id) {
    List<HTravelGroup> result =  travelGroupMapper.selectByTimeTableId(id);
    return result;
  }

  @Override
    public ServerResponse<FleaPage<HTravelGroup>> findAllIsNoGroupForPage(HttpServletRequest request,
                                                                          VisitorGroupVo visitorGroupVo, int curPage, int pageSize) throws Exception {
/*		if(null!=vvo&& null!=vvo.getDate()){
			List<HTimeTable> timeTables= timeTableMapper.findByDateForRecoder(vvo.getDate());
			if(null!=timeTables && timeTables.size()>0){
				vvo.setTimeTables(timeTables);
			}
		}*/
        RowBounds rowBounds = new RowBounds(curPage, pageSize);
        List<HTravelGroup> groups = travelGroupMapper.findAllGroupPaging(rowBounds, visitorGroupVo);
        FleaPage<HTravelGroup> result = new FleaPage<>(groups);
        //result.setCurPageData(groups);
        return ServerResponse.createBySuccess("成功", result);
    }

}
